package com.example.backend.controller;

import com.example.backend.entity.Company;
import com.example.backend.dto.CompanyPortInfo;
import com.example.backend.service.CompanyService;
import com.example.backend.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping
    public ApiResponse<String> addCompany(@RequestBody Company company) {
        companyService.addCompany(company);
        return ApiResponse.success("添加成功");
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping("/{id}")
    public ApiResponse<Company> getCompanyById(@PathVariable("id") Long id) {
        Company company = companyService.getCompanyById(id);
        if (company != null) {
            return ApiResponse.success(company);
        } else {
            return ApiResponse.error(404, "公司不存在");
        }
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping
    public ApiResponse<List<Company>> getAllCompanies() {
        List<Company> companies = companyService.getAllCompanies();
        return ApiResponse.success(companies);
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @PutMapping
    public ApiResponse<String> updateCompany(@RequestBody Company company) {
        companyService.updateCompany(company);
        return ApiResponse.success("更新成功");
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteCompany(@PathVariable("id") Long id) {
        companyService.deleteCompany(id);
        return ApiResponse.success("删除成功");
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping("/port/{portId}")
    public ApiResponse<List<Company>> getCompaniesByPortId(@PathVariable("portId") Long portId) {
        List<Company> companies = companyService.getCompaniesByPortId(portId);
        return ApiResponse.success(companies);
    }

    /**
     * 获取指定港口下的公司列表（包含剩余吨数）
     * 通过货存记录表查询，自动去重，只显示有剩余库存的公司
     */
    @GetMapping("/port/{portId}/with-remaining-weight")
    public ApiResponse<List<CompanyPortInfo>> getCompaniesWithRemainingWeightByPortId(@PathVariable("portId") Long portId) {
        List<CompanyPortInfo> companies = companyService.getCompaniesWithRemainingWeightByPortId(portId);
        return ApiResponse.success(companies);
    }

    /**
     * 获取指定港口下的公司列表（简单版本，不计算剩余吨数）
     * 通过货存记录表查询，自动去重
     */
    @GetMapping("/port/{portId}/simple")
    public ApiResponse<List<CompanyPortInfo>> getCompaniesByPortIdSimple(@PathVariable("portId") Long portId) {
        List<CompanyPortInfo> companies = companyService.getCompaniesByPortIdSimple(portId);
        return ApiResponse.success(companies);
    }

    /**
     * 查询公司在各港口的剩余货物
     */
    @GetMapping("/{companyId}/ports-with-remaining")
    public ApiResponse<List<CompanyPortInfo>> getPortsWithRemainingByCompanyId(@PathVariable("companyId") Long companyId) {
        List<CompanyPortInfo> list = companyService.getPortsWithRemainingByCompanyId(companyId);
        return ApiResponse.success(list);
    }
} 